package com.ludomancy.starwave;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import com.ludomancy.gamelib.Button;
import com.ludomancy.gamelib.DialogWidget;
import com.ludomancy.gamelib.GameCanvas;
import com.ludomancy.gamelib.Layout;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Widget;
import com.ludomancy.gamelib.Button.ButtonListener;
import com.ludomancy.gamelib.DialogWidget.DialogListener;

public class StarWaveWidget extends Widget implements DialogListener, ButtonListener
{
	public GridWidget grid;
	public MothershipWidget myMothership;
	private MothershipWidget hisMothership;
	public Button endTurnButton;
	public Button retryButton;
	public Button nextLevelButton;
	public InfobarWidget infoBar;
	private BriefingWidget winLevelMB;
	private BriefingWidget beginLevelMB;
	
	public StarWaveWidget(Widget parent, Rectangle area, int[][] initialSetup, boolean showBriefing)
	{
		super(parent, area);
		
		// calculate sizes
		int gridWidth = Settings.TILE_SIZE*Game.GRID_WIDTH+1;
		int gridHeight = Settings.TILE_SIZE*Game.GRID_HEIGHT+1;
		
		// info bar
		infoBar = new InfobarWidget(this, new Rectangle(0, 0, getWidth(), 40));
		
		// add the grid widget
		grid = new GridWidget(this, new Rectangle(0, 0, gridWidth, gridHeight), initialSetup);
		
		// add mothership bar
		myMothership = new MothershipWidget(this, new Rectangle(0, 0, Settings.TILE_SIZE, gridHeight), Game.game.me);
		hisMothership = new MothershipWidget(this, new Rectangle(0, 0, Settings.TILE_SIZE, gridHeight), Game.game.ai);		
		
		// add the "end turn" button
		endTurnButton = new Button(this, new Rectangle(myMothership.getWidth(), 0, gridWidth, 30), "Ready!", this);
		
		// set layout
		Widget[][] layouts = new Widget[][]
		{
				{infoBar},
				{myMothership, grid, hisMothership},
				{endTurnButton},
		};
		Layout.doLayout(layouts, 0, 5, 0, 0);
		retryButton = new Button(this, endTurnButton.getAbsoluteBounds(), "Try Again", this);
		retryButton.setVisible(false);
		nextLevelButton = new Button(this, endTurnButton.getAbsoluteBounds(), "Next...", this);
		nextLevelButton.setVisible(false);
		if(showBriefing && Game.game.currentMission.description != null)
		{
			showBriefing();
		}
	}
	
	public static boolean shiftPressed = false;
	
	public void keyPressed(KeyEvent e)
	{
		if(Game.EDITOR)
		{
			if(e.getKeyCode() == KeyEvent.VK_SHIFT)
			{
				shiftPressed = true;
			}
		}
	}

	public void keyReleased(KeyEvent e)
	{
		if(Game.EDITOR)
		{
			if(e.getKeyCode() == KeyEvent.VK_SHIFT)
			{
				shiftPressed = false;
			}
		}
	}

	public void keyTyped(KeyEvent e)
	{
		if(e.getKeyChar() == 'c')
		{
			// cheat
			levelEnd(true);
		}
	}
	
	public void showBriefing()
	{
		// show welcome message
		beginLevelMB = new BriefingWidget(this, Game.game.currentMission.description, "Start", this);
		GameCanvas.screen.showWidgetModal(beginLevelMB);		
	}
	
	public void update()
	{
	}
	
	public void paint(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		// background
		Image map = ResourceManager.getImage("background.png");
		g2.drawImage(map, 0, 0, null);
	}

	public void onPress(Button widget)
	{
		if(widget == endTurnButton)
		{
			Game.game.me.processTurn();
			Game.game.ai.processTurn();
			Game.game.endPhase();
		}
		else
		if(widget == retryButton)
		{
			Game.game.endPhase();
		}
		else
		if(widget == nextLevelButton)
		{
			Game.game.endPhase();
		}
	}
	
	public void levelEnd(boolean won)
	{
		if(won)
		{
			StarWaveApplet.app.nextLevel();
		}
		else
		{
			StarWaveApplet.app.resetGame(false, Game.game.me.initialSetup);
		}
	}
	
	public void onDialogClosed(DialogWidget dialog)
	{
		if(dialog == winLevelMB)
		{
			// finish level
			StarWaveApplet.app.nextLevel();
		}
	}
}
